﻿using FPSO.HMI.Pipe;
using FPSO.Models.Domain;
using FPSO.Models.ViewModel.Controls;
using FPSO.Services;
using FPSO.Services.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace FPSO.HMI.Controls
{
    /// <summary>
    /// UCOutAndSwapMin.xaml 的交互逻辑
    /// </summary>
    public partial class UCOutAndSwapMin : UserControl
    {
        private PipeGraph outAndSwapGraph = null;
        private DispatcherTimer taskTimer = null;
        //private UCOutAndSwapViewModel viewModel = null;
		private CommStatusViewModel viewModel = null;
		private ImitateExecuteTaskService<CommStatusViewModel> imitateService = null;
        //private IDataBindingService bindingService = null;

        public UCOutAndSwapMin()
        {
            InitializeComponent();

            viewModel = new CommStatusViewModel();
            DataContext = viewModel;

            taskTimer = new DispatcherTimer();
            taskTimer.Interval = TimeSpan.FromSeconds(1);
            taskTimer.Tick += TaskTimer_Tick;
            taskTimer.Start();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            if (outAndSwapGraph  == null)
            {
                outAndSwapGraph = new PipeGraph(outAndSwap);
            }
        }

        private void TaskTimer_Tick(object sender, EventArgs e)
        {
            if (outAndSwapGraph != null)
            {
                outAndSwapGraph.CalcPumpLines();
            }
        }

        public void ReflushViewModel(ManualCalcResult calcResult)
        {
            //暂时不启用PDB
            //IDataBindingService realDataService = new RealDataBindingServiceImpl<UCOutAndSwapViewModel>(SysConfig.PDBConnStr, viewModel);
			IDataBindingService realDataService = new RealDataBindingServiceImpl<CommStatusViewModel>(viewModel);
			realDataService.ReflushData(calcResult);
        }

        public void StartImitate()
        {
            imitateService = new ImitateExecuteTaskService<CommStatusViewModel>(viewModel, new DBTankTaskService());
        }

        public void SetImitateTime(DateTime lastRefreshTime, DateTime currentTime)
        {
            if (imitateService != null)
            {
                imitateService.ReflushData(lastRefreshTime,currentTime);
            }
        }
    }
}
